// pages/waybill/waybill.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    waybillList: [],
    listHeight: 0,
    status: null,     //物流状态： 0：待揽件、1：已揽件
    pagenation:{
      total_count: 0,      //总数量
      page_start: 1,      //当前页码
      page_size:  10,        //每页数量
      page_count: 0         //总页码
    },
    page_start: 1,      //当前页码
    page_size: 10,        //每页数量
    noData:false,
    show_dubious: false,     //是否显示可疑件弹出
    show_check:false,
    select_dubious_logistics_id: 0,        //选中的被标记可疑件订单ID
    select_dubious_index: 0,                //选中的被标记为可疑件的index 序号
    keywords:''
  },

  /**
   * tab切换
   */
  tabChange: function (e) {
    let name = e.detail.name;
    let pagenation = {
      page_start: 1,      //开始页码
      page_size: 10,      //每页数量
    }
    this.setData({ waybillList: []});
    if (name == 'all'){
      this.setData({
        status: null,
        show_check: false
      });
      this.listLogisticd(pagenation);
    }else{
      if (name == 'pending'){
        this.setData({ 
          status: 0,
          show_check:true,
          noData: false
         });
        this.listLogisticd(pagenation);
        
      } else if (name == 'pend'){
        this.setData({ 
          status: 1,
          show_check: false,
          noData: false
         });
        this.listLogisticd(pagenation);
      }   
    }
  },

  getchecklist: function (){
    let pagenation = {
      page_start: 1,      //开始页码
      page_size: 10,      //每页数量
    }
    this.listLogist(pagenation,0)
  },

  listLogist: function (pagenation, status) {
    let that = this;
    let post = pagenation;
    let mess = {}
    let reg = /^1[3-9]{1}\d{9}$/.test(this.data.keywords)
    if (reg){
      mess.post_communicate_mobile = this.data.keywords
      mess.status =status
      mess.pagenation = pagenation
    }else{
      mess.post_communicate_name = this.data.keywords
      mess.status = status
      mess.pagenation = pagenation
    }
    console.log(mess)
    wx.$api.listLogistics(mess).then(res => {
      if (res.data.code != 200) return wx.$Toast(res.data.msg);
      that.setData({
        waybillList: res.data.data.list
        // waybillList: that.data.waybillList.concat(res.data.data.list)
      });
    });

  },
  /**
   * 跳转详情界面
   */
  toDetail: function (e) {
    wx.navigateTo({
      url: '/pages/waybillDetail/waybillDetail?type=' + e.currentTarget.dataset.type + '&logistics_id=' + e.currentTarget.dataset.logistics_id,
    })
  },

  /**
   * 打开标记可疑件确认框
   */
  toDubious(e){
    this.setData({
      show_dubious: true,
      select_dubious_logistics_id: e.currentTarget.dataset.logistics_id,
      select_dubious_index: e.currentTarget.dataset.index
    });
  },

  /**
   * 关闭可疑件弹窗
   */
  closeDubious(){     //弹窗点击确认时，会先调用关闭函数，所以这里不能将 select_dubious_logistics_id、select_dubious_index 设为 0
    console.log('close_dubious');
    this.setData({
      show_dubious: false, 
    });
  },

  /**
   * 确认标记为可疑件
   * @param {*} options 
   */
  confirmDubious(){
    let that = this;
    wx.$api.doDubious({logistics_id: that.data.select_dubious_logistics_id}).then(res => {
      if(res.data.code != 200)    return  wx.$Toast(res.data.msg);
      let tmp_way_bill = that.data.waybillList.splice(that.data.select_dubious_index,1);
      console.log('waybillList111', that.data.waybillList);
      tmp_way_bill[0].is_dubious = 1;
      console.log('tmp_way_bill', tmp_way_bill);
      that.data.waybillList.splice(that.data.select_dubious_index,0, tmp_way_bill[0]);
      console.log('waybillList222', that.data.waybillList);
      that.setData({
        waybillList: that.data.waybillList
      })
    });

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //首次加载时，展示  全部 的数据
    let pagenation = {
      page_start: this.data.pagenation.current_page,      //开始页码
      page_size: this.data.pagenation.page_size,      //每页数量
    };
    this.listLogistics(pagenation, this.data.status);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.computeHeight('.waybill-tab', (height)=>{
      this.setData({
        listHeight: height
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log(1)
    this.listLogistics(pagenation, this.data.status);
  },
  lower: function () {
    this.setData({
      page_start: this.data.page_start + 1 //上拉到底时将page+1后再调取列表接口
    });
    this.listLogistics();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 揽件列表 
   */
  listLogistics: function (){
    let that = this;
    let pagenation = {
      page_start: that.data.page_start,      //开始页码
      page_size: that.data.page_size,      //每页数量
    }
    wx.$api.listLogistics({ status: that.data.status, pagination: pagenation}).then(res => {
      if(res.data.code != 200)    return  wx.$Toast(res.data.msg);
      const rowt = res.data.data.list
      if (rowt == null || rowt.length === 0) {
        that.setData({
          noData: true,
        })
        return;
      }
      if (that.data.page_start === 1) {
        that.setData({
          waybillList: res.data.data.list
        });
      } else {
        that.setData({
          waybillList: that.data.waybillList.concat(rowt)
        });
      }
      // that.setData({
      //   waybillList: that.data.waybillList.concat(res.data.data.list)
      // });

    });

  },
  listLogisticd: function (pagenation) {
    let that = this;
    wx.$api.listLogistics({ status: that.data.status, pagination: pagenation }).then(res => {
      if (res.data.code != 200) return wx.$Toast(res.data.msg);
      that.setData({
        waybillList: that.data.waybillList.concat(res.data.data.list)
      });

    });

  },
  onChange: function (e) {
    this.setData({
      keywords: e.detail
    })
  },
  onblur:function(){
    let reg = /^1[3-9]{1}\d{9}$/.test(this.data.keywords)
    if (reg) {
      console.log('这是手机号')
    } else {
      console.log('这是字符串')
    }
  }


})